//graph初始化
var graph = window.graph = new joint.dia.Graph;

//自定义link
joint.shapes.standard.Link.define('app.Link', {
    smooth: true,
    labels: [],
    attrs: {
        line: {
            stroke: '#20C1DD',
            strokeDasharray: '0',
            strokeWidth: 2,
            fill: 'none',
            sourceMarker: {
                type: 'path',
                d: 'M 0 0 0 0',
                stroke: 'none'
            },
            targetMarker: {
                type: 'path',
                d: 'M 0 -5 -10 0 0 5 z',
                stroke: 'none'
            }
        }
    }
}, {

    getMarkerWidth: function (type) {
        var d = (type === 'source') ? this.attr('line/sourceMarker/d') : this.attr('line/targetMarker/d');
        return this.getDataWidth(d);
    },

    getDataWidth: _.memoize(function (d) {
        return (new g.Path(d)).bbox().width;
    })

}, {

    connectionPoint: function (line, view, magnet, opt, type, linkView) {
        var markerWidth = linkView.model.getMarkerWidth(type);
        opt = {offset: markerWidth, stroke: true};
        // connection point for UML shapes lies on the root group containg all the shapes components
        if (view.model.get('type').indexOf('uml') === 0) opt.selector = 'root';
        return joint.connectionPoints.boundary.call(this, line, view, magnet, opt, type, linkView);
    }
});
//paper初始化
var paper = window.paper = new joint.dia.Paper({
    width: 2000,
    height: 1500,
    model: graph,
    gridSize: 1,
    snapLinks: true,
    linkPinning: false,
    defaultLink: new joint.shapes.app.Link({
        //  router: { name: 'manhattan' },
        // connector: { name: 'rounded' },
        smooth: true,
        attrs: {
            '.connection': {
                stroke: '#20C1DD',
                'stroke-width': 2,
                pointerEvents: 'none',
            },
            '.marker-target': {
                fill: '#20C1DD',
                stroke: '#fff',
                d: 'M 10 0 L 0 5 L 10 10 z'
            }
        },
    }),
    defaultConnectionPoint: joint.shapes.app.Link.connectionPoint
});
//paper背景
var bgimage = ''
paper.drawBackground({
    image: bgimage
});

//控制纸张
var paperScroller = new joint.ui.PaperScroller({
    paper: paper,
    autoResizePaper: true,
    padding: 50,
    cursor: 'grab'
});
// 移动纸张
paper.on('blank:pointerdown', paperScroller.startPanning);
$('#paper').append(paperScroller.render().el);
paperScroller.center();    //移动到页面的中间

//元素间的对齐线
var snaplines = new joint.ui.Snaplines({paper: paper});
snaplines.startListening();

//鹰眼模块
var nav = new joint.ui.Navigator({
    paperScroller: paperScroller,
    width: 133,
    height: 100,
    padding: 5,
    model: graph,
    gridSize: 1,
});
nav.$el.appendTo('#myholder-small');
nav.render();

//tooltip模块
new joint.ui.Tooltip({
    target: '[data-tooltip]',
    rootTarget: document.body,
    direction: 'auto',
    padding: 15,
});

//得到字符串的长度
function getStringLen(str) {
    return str.replace(/[\u0391-\uFFE5]/g, "aa").length;
}

//得到所有出站的元素
function getOutboundElement(elem, direction) {

    var list = [], //元素id数组
        eleList = []; //元素数组
    list.push(elem.id); //排除当前元素id

    return (function findRelateModel() {     //子元素相关的所有元素
        var NeighborsBPMNArr = graph.getNeighbors(elem, direction);  //相邻元素数组
        $.each(NeighborsBPMNArr, function (i, v) {        //遍历相关元素
            if (list.indexOf(v.id) === -1) {
                list.push(v.id);
                eleList.push(v);
                elem = v;                //当前元素
                findRelateModel();       //回调 那不是
            }
        });
        //业务逻辑
        $.each(eleList, function (i, v) { //删除其他所有的元素
            v.remove();
        });
    })()
}

//得到所有关联的元素
function getRelationElements(object, callback) {

    var currentElement = object.element;
    var direction = object.direction;
    var list = [];  //元素id数组
    list.push(currentElement.id); //排除当前元素
    findRelateModel(currentElement);

    function findRelateModel(currentElement) {     //子元素相关的所有元素
        var NeighborsBPMNArr = graph.getNeighbors(currentElement, direction);  //相邻元素数组
        $.each(NeighborsBPMNArr, function (i, v) {        //遍历相关元素
            if (list.indexOf(v.id) === -1) {
                list.push(v.id);
                callback && callback(v);
                findRelateModel(v);               //回调
            }
        })


    }
}